package cn.ekuma.epos.datalogic.businesslogic.orderprocess.impl;

import com.openbravo.bean.crm.CustomerProduct;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderLine;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.data.basic.BasicException;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.AbstractOrderProcess;

public class CustomerProductOrderProcess extends AbstractOrderProcess {

	@Override
	public String getName() {
		return "CustomerProductOrderProcess";
	}

	@Override
	public void saveOrder(I_DataLogicERP dlERP, Order order)
			throws BasicException {
	    OrderType oType = order.getDefineType();
		String customerId=order.getCustomerId();
		if(oType.getInOutType()==OrderType.STORE_NoChange||customerId==null)
			return;
		String moveType=oType.getName();
		CustomerProduct customerProduct;
		boolean isInType=false;
		 if (OrderType.STORE_In == oType.getInOutType()&&Order.ORDERBEARING_NORMAL == order.getOrderType())
            	 isInType=true;
           else if (OrderType.STORE_Out == oType.getInOutType()&&Order.ORDERBEARING_REVERSE == order.getOrderType()) {
        	   isInType=true;
         } else if (OrderType.STORE_BalanceIn == oType.getInOutType()) {
        	 isInType=true; 
         }
		for (OrderLine l : order.getLines()) {
			if (l.getProductID() != null) {
				customerProduct=new CustomerProduct();
				customerProduct.setCustomerId(customerId);
				customerProduct.setProductId(l.getProductID());
				if(isInType){
					customerProduct.setPriceIn(l.getPrice());
					customerProduct.setTimeIn(order.getDate());
					customerProduct.setReasonIn(moveType);
					dlERP.productUpdateIn(customerProduct);
				}else{
					customerProduct.setPriceOut(l.getPrice());
					customerProduct.setTimeOut(order.getDate());
					customerProduct.setReasonOut(moveType);
					dlERP.productUpdateOut(customerProduct);
				}
			}
		}
	}

	
}
